<template>
  <div id="globalHeader">
      <a-row :wrap="false">
        <a-col flex="200px">
          <RouterLink to="/">
            <div class="title-bar">
              <img class="logo" src="../../assets/logo.png" alt="logo" />
              <div class="title">璃AI应用构建</div>
            </div>
          </RouterLink>
        </a-col>
        <a-col flex="auto">
          <a-menu
            v-model:selectedKeys="current"
            mode="horizontal"
            :items="menuItems"
            @click="doMenuClick"
          />
        </a-col>
        <!-- 用户信息下拉展示 -->
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown>
            <a-space>
              <a-avatar :src="loginUserStore.loginUser.userAvatar" />
              {{loginUserStore.loginUser.userName ?? '无名'}}
            </a-space>
              <template #overlay>
                <a-menu>
                  <a-menu-item>
                    <router-link to="/user/personalMessage">
                      <UserOutlined />
                      个人信息
                    </router-link>
                  </a-menu-item>
                  <a-menu-item>
                    <router-link to="/user/resetPassword">
                      <UnlockOutlined />
                      修改密码
                    </router-link>
                  </a-menu-item>
                  <a-menu-item @click="doLogout">
                    <LogoutOutlined />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-row>
  </div>
</template>

<script lang="ts" setup>
import { computed, h, ref } from 'vue'
import { HomeOutlined } from '@ant-design/icons-vue'
import { MenuProps, message } from 'ant-design-vue'
import { useRouter } from "vue-router";
import {useLoginUserStore} from '@/stores/loginUser.ts'
import { LogoutOutlined ,UserOutlined,UnlockOutlined} from '@ant-design/icons-vue/lib/icons'
import { userLogout } from '@/api/userController.ts'
import checkAccess from '@/access/checkAccess.ts'
import ACCESS_ENUM from '@/access/accessEnum.ts'



const loginUserStore = useLoginUserStore()

const router = useRouter();
// 路由跳转事件
const doMenuClick = ({ key }: { key: string }) => {
  router.push({
    path: key,
  });
};
const current = ref<string[]>(['home'])
router.afterEach((to,from,next)=>{
  current.value=[to.path];
});
/**
 * 用户注销
 *
 * @param null
 * @return
 * @author lyx
 * @create 2025/10/24
 **/
const doLogout = async () => {
  const res = await userLogout()
  if (res.data.code === 0) {
    loginUserStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出登录成功')
    await router.push('/user/login')
  } else {
    message.error('退出登录失败，' + res.data.message)
  }
}

//将菜单转换为路由项
const menuToRouteItem = (menu: MenuProps['items'][0]) => {
  return{
    path: menu.key,
    meta: {
      //根据路径判权
      access: menu.key?.toString().startsWith('/admin')?
        ACCESS_ENUM.ADMIN: menu.key?.toString()
                            !=='/'?ACCESS_ENUM.USER:ACCESS_ENUM.NOT_LOGIN,
        hideInMenu: false,
    }
  }
}


const originItems = ref<MenuProps['items']>([
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/admin/userManage',
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/admin/appManage',
    label: '应用管理',
    title: '应用管理',
  },
  {
    key: '/admin/chatManage',
    label: '对话管理',
    title: '对话管理',
  },
])
//过滤菜单项
const filterMenus = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu) => {
    const item = menuToRouteItem(menu);
    if(item.meta?.hideInMenu){
      return false;
    }
    return checkAccess(loginUserStore.loginUser,item.meta?.access);
  })
}

const menuItems = computed(()=>filterMenus(originItems.value))
</script>


<style scoped>

.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}

.logo {
  height: 48px;
}
</style>
